package com.thrd.basicframework.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.view.ViewCompat;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: ${唐杰}
 * @date: 2021/3/15
 */
public class TextLargeImageView extends View {
    private BitmapRegionDecoder mDecoder;
    /**
     * 图片的宽度和高度
     */
    private int mImageWidth, mImageHeight;
    /**
     * 绘制的区域
     */
    private volatile Rect mRect = new Rect();
    /**
     * 显示的区域
     */
    private volatile Rect mShowRect = new Rect();

    private MoveGestureDetector mDetector;


    private static final BitmapFactory.Options options = new BitmapFactory.Options();

    static
    {
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    }

    private int width;
    private int height;


    public void setInputStream(InputStream is)
    {
        try
        {

            BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
            // Grab the bounds for the scene dimensions
            tmpOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, tmpOptions);
            mImageWidth = tmpOptions.outWidth;
            mImageHeight = tmpOptions.outHeight;
            mDecoder = BitmapRegionDecoder.newInstance(is, false);




            requestLayout();
            invalidate();

//            new Thread(new Runnable(){
//
//                @Override
//                public void run() {
//                    while (true){
//
//                        invalidate();
//                        notifyInvalidate();
//                    }
//
//                }
//            }).start();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {

            try
            {
                if (is != null) is.close();
            } catch (Exception e)
            {
            }
        }
    }


    public void init()
    {
        mDetector = new MoveGestureDetector(getContext(), new MoveGestureDetector.SimpleMoveGestureDetector()
        {

            private int moveX;
            private int moveXX;

            @Override
            public boolean onMove(MoveGestureDetector detector)
            {
                 moveX = (int) ((int) detector.getMoveX()*0.8);
              //  int moveY = (int) ((int) detector.getMoveY()*0.5);
                moveXX = -(int) ((int)detector.getMoveX()*0.8);
                if (mImageWidth > getWidth())
                {
                   // if(moveX)

                    mShowRect.offset(moveX, 0);
                    mRect.offset(-moveX,0);
//                    if(mShowRect.left>0){
//                        mShowRect.left = 0;
//                        mShowRect.top = 0;
//                        mShowRect.right = mShowRect.left+width ;
//                        mShowRect.bottom = mShowRect.top + height;
//                    }
                    postInvalidate();
//                    notifyInvalidate();
//                    scrollTo(moveX,0);
//                    checkWidth();
//                    layout(mShowRect.left,mShowRect.top,mShowRect.right,mShowRect.bottom);
//                    notifyInvalidate();

                }
//                if (mImageHeight > getHeight())
//                {
//                    mRect.offset(0, -moveY);
//                    checkHeight();
//                    invalidate();
//                }

                return true;
            }

            @Override
            public void onMoveEnd(MoveGestureDetector detector) {
                super.onMoveEnd(detector);
//                layout(0,mRect.top,width,mRect.bottom);
//                mRect.offset(-moveX, 0);
//                mRect.offset(moveXX, 0);
//                layout(0,mShowRect.top,width,mShowRect.bottom);
////              //  checkWidth();
//                notifyInvalidate();
            }
        });
    }


    private void checkWidth()
    {


        Rect rect = mRect;
        int imageWidth = mImageWidth;
        int imageHeight = mImageHeight;

        if (rect.right > imageWidth)
        {
            rect.right = imageWidth;
            rect.left = imageWidth - getWidth();
        }

        if (rect.left < 0)
        {
            rect.left = 0;
            rect.right = getWidth();
        }
    }


    private void checkHeight()
    {

        Rect rect = mRect;
        int imageWidth = mImageWidth;
        int imageHeight = mImageHeight;

        if (rect.bottom > imageHeight)
        {
            rect.bottom = imageHeight;
            rect.top = imageHeight - getHeight();
        }

        if (rect.top < 0)
        {
            rect.top = 0;
            rect.bottom = getHeight();
        }
    }


    public TextLargeImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        mDetector.onToucEvent(event);
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        Bitmap bm = mDecoder.decodeRegion(mRect, options);
        canvas.drawBitmap(bm, 0, 0, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width = getMeasuredWidth();
        height = getMeasuredHeight();

        int imageWidth = mImageWidth;
        int imageHeight = mImageHeight;

        //默认直接显示图片的中心区域，可以自己去调节
//        mRect.left = imageWidth / 2 - width / 2;
//        mRect.top = imageHeight / 2 - height / 2;
        mRect.left = 0;
        mRect.top = 0;
        mRect.right = (int) (mRect.left + (width ));
        mRect.bottom = mRect.top + height;


        mShowRect.left = 0;
        mShowRect.top = 0;
        mShowRect.right = (int) (mShowRect.left + (width ));
        mShowRect.bottom = mShowRect.top + height;


    }


    private void notifyInvalidate() {
        ViewCompat.postInvalidateOnAnimation(this);
    }

}
